package com.abbas.wallerpaper.fragment;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.abbas.wallerpaper.R;
import com.abbas.wallerpaper.base.BaseFragment;
import com.abbas.wallerpaper.entity.ImageBean;
import com.abbas.wallerpaper.http.IRetrofit;
import com.abbas.wallerpaper.ui.Detail2Activity;
import com.abbas.wallerpaper.ui.DetailActivity;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import me.dkzwm.widget.srl.MaterialSmoothRefreshLayout;
import me.dkzwm.widget.srl.RefreshingListenerAdapter;
import me.dkzwm.widget.srl.indicator.IIndicator;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class ContainFragment extends BaseFragment {

    private RecyclerView rc;
    private ProgressBar progressbar;

    private MaterialSmoothRefreshLayout refresh;

    private OnItemClickListener listener = new OnItemClickListener() {
        @Override
        public void itemClick(ImageBean.ResBean.VerticalBean entityBean) {
            Intent intent = new Intent(getActivity(), Detail2Activity.class);
            intent.putExtra("data", entityBean);
            startActivity(intent);
        }
    };


    @Override
    protected int getLayoutId() {
        return R.layout.fragment_contain;
    }

    @Override
    protected void init() {
        rc = (RecyclerView) rootView.findViewById(R.id.rc);
        progressbar = rootView.findViewById(R.id.progress_circular);
        refresh = rootView.findViewById(R.id.rl);

        refresh.setEnablePullToRefresh(true);
        refresh.setEnableAutoLoadMore(true);
        rc.setLayoutManager(new StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.VERTICAL));

    }

    private List<ImageBean.ResBean.VerticalBean> resBean;
    private RcAdapter rcAdapter;

    @Override
    protected void initEvent() {

        refresh.setOnRefreshListener(new RefreshingListenerAdapter() {
            @Override
            public void onRefreshing() {
                showTip(getView(), "触发刷新");

                resBean.clear();
                getData();
            }

            @Override
            public void onLoadingMore() {
                showTip(getView(), "触发加载");
                getData();
            }
        });

    }

    @Override
    protected void initData() {

        getData();
    }

    private void getData() {
        IRetrofit.getInstance().getImage().enqueue(new Callback<ImageBean>() {
            @Override
            public void onResponse(Call<ImageBean> call, Response<ImageBean> response) {
                progressbar.setVisibility(View.GONE);
                resBean = response.body().getRes().getVertical();
                if (rcAdapter == null) {
                    rcAdapter = new RcAdapter();
                    rc.setAdapter(rcAdapter);
                } else {
                    int before = resBean.size();
                    resBean.addAll(response.body().getRes().getVertical());
                    int later = resBean.size();
                    rcAdapter.notifyItemRangeChanged(before, later);
                }
                if (refresh.isRefreshing()) {
                    refresh.refreshComplete();
                }
                Toast.makeText(requireActivity(), String.valueOf(resBean.size()), Toast.LENGTH_SHORT).show();
                ;
//                callBack.onSuccess();
            }

            @Override
            public void onFailure(Call<ImageBean> call, Throwable t) {
//                callBack.onFailure();
                if (refresh.isRefreshing()) {
                    refresh.refreshComplete();
                }
            }
        });
    }

    class RcAdapter extends RecyclerView.Adapter<ViewHolder> {

        @NonNull
        @Override
        public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(getActivity()).inflate(R.layout.recycle_item, parent, false);
            return new ViewHolder(view);
        }

        @Override
        public void onBindViewHolder(@NonNull ViewHolder holder, @SuppressLint("RecyclerView") int position) {
            Glide.with(getActivity())
                    .load(resBean.get(position).getImg())
                    .transform(new RoundedCorners(8))
                    .into(holder.imageView);
            List<String> tag = resBean.get(position).getTag();
            if (tag != null && tag.size() > 0) holder.textView.setText(tag.get(0));


            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    listener.itemClick(resBean.get(position));
                }
            });
        }

        @Override
        public int getItemCount() {
            return resBean.size();
        }
    }

    static class ViewHolder extends RecyclerView.ViewHolder {

        private ImageView imageView;
        private TextView textView;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);

            imageView = itemView.findViewById(R.id.image);
            textView = itemView.findViewById(R.id.tv);

        }
    }

    public static class EntityBean implements Serializable {
        private int resId;
        private String desc;

        public EntityBean(int resId, String desc) {
            this.resId = resId;
            this.desc = desc;
        }

        public int getResId() {
            return resId;
        }

        public void setResId(int resId) {
            this.resId = resId;
        }

        public String getDesc() {
            return desc;
        }

        public void setDesc(String desc) {
            this.desc = desc;
        }
    }

    interface OnItemClickListener {
        void itemClick(ImageBean.ResBean.VerticalBean bean);
    }

    interface CallBack {
        void onSuccess();

        void onFailure();
    }
}